package com.pcbly.web.persistence.form;

import java.util.List;

import com.pcbly.web.bean.field.FormTableDataBean;
import com.pcbly.web.components.utils.random.GUID;
import com.pcbly.web.persistence.HibernateBaseDao;

@SuppressWarnings("unchecked")
public class FormTableDataHibernateDao extends
		HibernateBaseDao<FormTableDataBean> implements FormTableDataDao {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4329601769497186088L;

	public FormTableDataHibernateDao() {
		super();
		// TODO Auto-generated constructor stub
	}

	protected Class<FormTableDataBean> getEntityClass() {
		return FormTableDataBean.class;
	}

	@Override
	public List<FormTableDataBean> queryFormTableDataBeansByFormId(String formId)
			throws Exception {
		// TODO Auto-generated method stub 
		List<FormTableDataBean> beans = getSession().createSQLQuery(
				"select * from t_form_table_data where FORMID=?").addEntity(
				FormTableDataBean.class).setString(0, formId).list();
		return beans;
	}

	@Override
	public List<FormTableDataBean> saveFormTableDataBeans(
			List<FormTableDataBean> beans) throws Exception {
		// TODO Auto-generated method stub
		for(FormTableDataBean bean:beans){
			String id=(new GUID()).toString();
			bean.setId(id);
			getSession().save(bean);
		}
		return beans;
	}
	
	@Override
	public List<FormTableDataBean> queryFormTableDataBeansByFormIdAndId(
			String formId, String dataId) throws Exception {
		// TODO Auto-generated method stub
		if(logger.isDebugEnabled()){ 
			logger.debug(formId);
			logger.debug(dataId);
		}
		try {
			List<FormTableDataBean> beans = getSession().createSQLQuery(
					"select * from t_form_table_data where FORMID=? and ID=?")
					.addEntity(FormTableDataBean.class).setString(0, formId)
					.setString(1, dataId).list(); 
			
			return beans;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
